﻿using DanTriNews.ViewModels;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DanTriNews.Src.Helper
{
    public class NewsHelper
    {
        public static async Task<ObservableCollection<NewsViewModel>> GetNews(int id, int page)
        {
            ObservableCollection<NewsViewModel> result = new ObservableCollection<NewsViewModel>();
            try
            {
                HtmlDocument d = await CrawlerHelper.GetDocument(id, page);
                HtmlNodeCollection ncLi = d.DocumentNode.SelectNodes("li");
                int idNews = 1;
                foreach (HtmlNode node in ncLi)
                {
                    try
                    {
                        string title = node.SelectSingleNode(".//h3").InnerText;
                        string imageUrl = node.SelectSingleNode("a/img").Attributes["src"].Value;
                        string date = node.SelectSingleNode("a/div/i").InnerText;
                        string detailUrl = "http://m.dantri.com.vn" + node.SelectSingleNode("a").Attributes["href"].Value; ;
                        NewsViewModel n = new NewsViewModel(idNews, title, imageUrl, detailUrl, date);
                        result.Add(n);
                        idNews++;
                    }
                    catch { }
                }
            }
            catch { }

            return result;
        }

        internal async static Task<string> getNewsContent(NewsViewModel nv)
        {
            try
            {
                if (string.IsNullOrEmpty(nv.NewsContent))
                {
                    HtmlDocument d = await CrawlerHelper.GetDocument(nv.DetailUrl);
                    HtmlNode node = d.DocumentNode.SelectSingleNode("//div[@id='wrapper']/div[@class='ovh detail_w']");
                    if (node != null)
                    {
                        RemoveNode(node.SelectSingleNode("h1"));
                        RemoveNode(node.SelectSingleNode("div[@class='publishdate']"));
                        RemoveNode(node.SelectSingleNode("div[@class='ovh social']"));
                        RemoveNode(node.SelectSingleNode("p"));
                    }
                    RemoveNode(node.SelectSingleNode("//div[@id='admwrapper']/div[1]"));
                    RemoveNode(d.DocumentNode.SelectSingleNode("//div[@class='cf submn_w']"));
                    RemoveNode(d.DocumentNode.SelectSingleNode("//div[@class='ovh']"));
                    RemoveNode(d.DocumentNode.SelectSingleNode("//iframe"));
                    RemoveNode(d.DocumentNode.SelectSingleNode("//div[@class='ovh cath']"));
                    RemoveNode(d.DocumentNode.SelectSingleNode("//div[@class='ovh dnn_w']"));
                    RemoveNode(d.DocumentNode.SelectSingleNode("//div[@class='ovh ttsk_w ttsk_d_w']"));
                    RemoveNode(d.DocumentNode.SelectSingleNode("//div[@class='adm']"));
                    RemoveNode(d.DocumentNode.SelectSingleNode("//div[@class='ovh footer_w']"));
                    RemoveNode(d.DocumentNode.SelectSingleNode("//a[@class='ovh view_more']"));
                    RemoveNode(d.DocumentNode.SelectSingleNode("//ul[@class='ovh lstv_o_w']"));
                    HtmlNodeCollection nodec = d.DocumentNode.SelectNodes("//img");
                    if (nodec != null && nodec.Count > 0)
                        foreach (HtmlNode n in nodec)
                        {
                            n.Attributes.Add("width", "100%");
                        }
                    nv.NewsContent = d.DocumentNode.InnerHtml;
                }
            }
            catch { }
            return nv.NewsContent;
        }
        private static void RemoveNode(HtmlNode node)
        {
            if (node != null)
                node.Remove();
        }

        //internal async static Task<string> getNewsContentVideo(NewsViewModel nv)
        //{
        //    try
        //    {
        //        if (string.IsNullOrEmpty(nv.NewsContent))
        //        {
        //            HtmlDocument d = await CrawlerHelper.GetDocument(nv.DetailUrl);
        //            HtmlNodeCollection nodes = d.DocumentNode.SelectNodes("//div[@id='admwrapper']/div");
        //            //RemoveNode(nodes[0]);
        //            //RemoveNode(nodes[1]);
        //            //RemoveNode(nodes[3]);
        //            nv.NewsContent = d.DocumentNode.InnerHtml;
        //        }
        //    }
        //    catch { }
        //    return nv.NewsContent;
        //}
    }
}
